package com.supor.aiot.mine.ui.ips;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;

import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.Util;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.base.NoViewModel;
import com.supor.aiot.common.helper.GridItemDecoration;
import com.supor.aiot.common.utils.BitmapUtil;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.ImageItem;
import com.supor.aiot.mine.bean.IpsBean;
import com.supor.aiot.mine.databinding.ActivityIpsDetailBinding;
import com.supor.aiot.mine.ui.adapter.UploadImageAdapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import static com.supor.aiot.mine.ui.ips.IpsActivity.idMap;

public class IpsDetailActivity extends BaseMVVMActivity<NoViewModel, ActivityIpsDetailBinding> implements View.OnLongClickListener {
    private IpsBean ipsBean;
    private UploadImageAdapter uploadImageAdapter;
    private List<ImageItem> itemList = new ArrayList<>();

    @Override
    public int resourceId() {
        return R.layout.activity_ips_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        ipsBean = (IpsBean) getIntent().getSerializableExtra(Constants.Key.BEAN);

        if (ipsBean == null) {
            finish();
            return;
        }

        Logc.d("initView: " + ipsBean.toString());
        if (ipsBean.modelId != null) {
            Integer id = idMap.get(ipsBean.modelId);
            if (id != null) {
                BitmapUtil.loadLocal(id, bindingView.ivIcon);
            }
        }
        bindingView.tvName.setText(ipsBean.modelName);
        if (!TextUtils.isEmpty(ipsBean.createTime)) {
            Date date = Util.parseDate(ipsBean.createTime);
            if (date != null) {
                bindingView.tvTime.setText("提交时间：" + Util.getTime(date));
            }
        }
        bindingView.etIpsDes.setText(ipsBean.ipsDescription);


        String images = ipsBean.ipsImages;
        if (!TextUtils.isEmpty(images) && images.length() > 2) {
            String[] imageArray = images.split(",");
            for (String s : imageArray) {
                ImageItem imageItem = new ImageItem();
                imageItem.type = 0;
                imageItem.imageDate = 1;
                imageItem.imagePath = s.trim();
                itemList.add(imageItem);
            }
        }
        String video = ipsBean.ipsVideoFiles;
        if (!TextUtils.isEmpty(video)) {
            ImageItem imageItem = new ImageItem();
            imageItem.type = 1;
            imageItem.imageDate = 1;
            imageItem.imagePath = video.trim();
            itemList.add(imageItem);
        }

        initPhotoRecyclerView();
    }

    @Override
    protected void initData() {
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());
    }

    private void initPhotoRecyclerView() {
        // 设置布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        bindingView.rcvPhoto.setLayoutManager(gridLayoutManager);
        bindingView.rcvPhoto.addItemDecoration(new GridItemDecoration(ContextCompat.getColor(this,
                R.color.transparent), getResources().getDimensionPixelSize(R.dimen.dp_4),
                getResources().getDimensionPixelSize(R.dimen.dp_6), -1));
        // 设置适配器
        uploadImageAdapter = new UploadImageAdapter(this, itemList);
        bindingView.rcvPhoto.setAdapter(uploadImageAdapter);
    }

    @SuppressLint("CheckResult")
    public void click(View v) {
        int id = v.getId();
    }

    @Override
    public boolean onLongClick(View v) {
        int id = v.getId();
        return false;
    }

}
